package top.vanchor.myblog2back.config;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2021/11/10 13:26
 */
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.vanchor.myblog2back.utils.JwtInterceptor;

@Configuration
public class webConfig  implements WebMvcConfigurer {
    //配置跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new JwtInterceptor());
        // 需拦截的路径
        interceptorRegistration.addPathPatterns("/**");
        interceptorRegistration.excludePathPatterns("/**.jpg","/**.jpeg","/**.png","/**.gif");
//        测试代码需要取消注释。因为这个拦截器会在代码出BUG的时候走未登录接口！
//        interceptorRegistration.excludePathPatterns("/**");
        // 需放行的路径------------------------------------------------------------------------
        //登录，未登录
        interceptorRegistration.excludePathPatterns("/**/login", "/**/unlogin","/**/sendShortSMS","/public/**","/**/register","/admin/adminUser/login",
                //所有博客列表，博客详情，博客评论By博客Id，获取搜索的博客，浏览博客，搜索用户
                "/blogBlog/getAllBlogJson","/blogBlog/getBlogByBlogId","/blogComment/getCommentTreeById","/blogBlog/getSearchBlogJson","/blogBlog/addViewTime","/blogUser/searchUsers",
                "/extra/**");
    }
}
